import axios, { AxiosRequestHeaders, AxiosRequestConfig } from 'axios';


const request = axios.create({
	// 超时超时
	timeout: 10000,
	timeoutErrorMessage: '链接超时....'
})

request.interceptors.request.use((config) => {
	// if(config.headers)
	config.headers!.token = localStorage.getItem('token') || ''

	return config
}, (error) => {
	return Promise.reject(error)
})

request.interceptors.response.use((response) => {
	const { code, data, message } = response.data
	// if(code === 0) {
	// 	console.log('请求成功');
	// }
	// 前后端在双方的约定
	switch (code) {
		case 0:
			console.log('请求成功');
			break
		case 1:
			console.log('请求参数的类型不正确');
			break
		case 2:
			console.log('缺少必要的参数');
			break
		case 3:
			console.log('权限不够');
			break
	}
	return response
}, (error) => {
	const { status } = error.response

	switch(status) {
		case 404:
			console.log('资源找不到');
			break
		case 500:
			console.log('网络服务器出问题了')
			break
	}

	return Promise.reject(error)
})





export default request
